const { tokenKey, tokenExpire } = require('../config')
const JWT = require('jsonwebtoken')

// 验证token
function verifyToken(token) {
    return new Promise((resolve, reject) => {
        JWT.verify(token, tokenKey, (err, decode) => {
            err ? reject(err) : resolve(decode)
        })
    })
}

// 签发token
function signToken(obj) {
    return new Promise((resolve, reject) => {
        JWT.sign(obj, tokenKey, { expiresIn: tokenExpire }, (err, encode) => {
            err ? reject(err) : resolve(encode)
        })
    })
}

module.exports = {
    signToken, verifyToken
}